home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++,rb.technical
- Path: yama.mcc.ac.uk!liv!news
- From: nickc@liv.ac.uk (Spider plant breeding program)
- Subject: What is "placement new" - was Re: Can copy constructor and operator= share code?
- In-Reply-To: Frederic LACHASSE's message of Sat, 02 Mar 1996 23:18:20 +0000
- Message-ID: <NICKC.96Mar7142042@uxe.liv.ac.uk>
- Sender: news@liverpool.ac.uk (News System)
- Nntp-Posting-Host: uxe.liv.ac.uk
- Organization: M-6 M-4 M-x hanoi
- References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM> <VA.00000053.00cdab05@fred>
- Date: Thu, 7 Mar 1996 14:20:42 GMT
-
- In article <VA.00000053.00cdab05@fred> Frederic LACHASSE <lachass@worldnet.fr> writes:
-
- > From: Frederic LACHASSE <lachass@worldnet.fr>
- > Generally, the operator=() must release old resource and create a copy
- > of the other object. So a generic operator=() can be:
- >
- > T &T::operator =(const T &t)
- > {
- > if (this != &t) // if objects are same, nothing to do
- > {
- > ~T(); // explicit call to destructor to release resources.
-
- Shouldn't this be this->~T();
-
- > new(this) T(t); // use of the placement operator to call the
- > // copy constructor.
- > }
-
- return *this;
-
- > }
- >
- > Some times though, the assignement operator can be optimize to reuse
- > resources of the old object.
-
- What is ths placement new operator?
-
- It's mentioned in passing in the FAQ, but never defined.
-
- No compiler I can run recognises this syntax
- (I've tried gcc 2.6.3, Solaris C++ (V4 I think), Irix C++)
-
- The problem I'm trying to solve is:
-
- class a
- {
- int *i;
- const double &d; // Actually something more complex
-
- public:
- a( const double &d_ ) : i( new int[3] ), d( d_ ) {};
- ~a() { delete i; };
- };
-
- How do I write operator = ( const class &a ) ?
-
- Thanks in advance,
-
- Nicholas Clark
- --
- C:\> ECHO f 0000:0000 ffff 66 | DEBUG
-